﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using Lib_general;

namespace Lib_general
{
    public class DmNhanvien
    {
        private Exec_Database exec=new Exec_Database();
        private DataSet dsnv = new DataSet();
        private SqlCommand cmd;
        private SqlConnection con;

        public DataSet Load_form()
        {
            try
            {
                string sql = "select manv,ten from dmnhanvien";
                dsnv = exec.Get_data(sql);
            }
            catch { return null; }
            return dsnv;
        }        
        public bool upd_dmnhanvien(string manv,string tennv,int status)
        {
            if (status == 0)
            {
                if (exec.Get_data("select manv from dmnhanvien where manv='" + manv + "'").Tables[0].Rows.Count > 0)
                    return false;
            }
            try
            {
                con = new SqlConnection(exec.s_connect);
                string sql = "";
                sql = "update dmnhanvien set ten=@tennv where manv=@manv";
                con.Open();
                cmd = new SqlCommand(sql,con);
                cmd.CommandType = CommandType.Text;
                cmd.Parameters.Add("@tennv", SqlDbType.NVarChar).Value = tennv;
                cmd.Parameters.Add("@manv", SqlDbType.VarChar).Value = manv;
                int irecord = cmd.ExecuteNonQuery();
                if (irecord == 0)
                {
                    sql = "insert into dmnhanvien(manv,ten) values(@manv,@tennv)";
                    cmd = new SqlCommand(sql,con);
                    cmd.CommandType = CommandType.Text;
                    cmd.Parameters.Add("@manv", SqlDbType.VarChar).Value = manv;
                    cmd.Parameters.Add("@tennv", SqlDbType.NVarChar).Value = tennv;
                    cmd.ExecuteNonQuery();
                }
            }
            catch { return false; }
            finally { cmd.Dispose(); con.Close(); con.Dispose(); }
            return true;
        }
        public bool delete_dmnhanvien(string manv)
        {
            try
            {
                string sql = "";
                sql = "select id from nhapth where userid='" + manv + "'";
                if (exec.Get_data(sql).Tables[0].Rows.Count > 0) return false;
                else
                {
                    sql = "delete from dmnhanvien where manv='" + manv + "'";
                    exec.Exec_data(sql);
                    return true;
                }
            }
            catch { return false; }
        }
    }
}
